﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// 单个格子的脚本
/// </summary>
public class ChangePic : MonoBehaviour
{
    public bool isHit = false;

    private Image pic;

    // 是否是可以翻转的牌
    [SerializeField]
    private bool canChange = false;
    //属性 控制翻转
    public bool CanChange
    {
        get { return canChange; }
        set
        {
            canChange = value;
            if (canChange)
            {
                //翻转出来的图片
                pic.sprite = GameManager.Instance.openedSprite;
                //放大和缩小
                StartCoroutine(CRScaleAnim());

                StartCoroutine(SetDefaultSprite(true, GameManager.Instance.changePicAfter));
            }
        }
    }




    void Awake()
    {
        pic = gameObject.GetComponent<Image>();
        pic.sprite = GameManager.Instance.defaultSprite;
    }

    #region 翻转之后执行的代码
    IEnumerator SetDefaultSprite(bool scaleEffect, float delay = 0f)
    {
        yield return new WaitForSeconds(delay);
        pic.sprite = GameManager.Instance.defaultSprite;
        if (scaleEffect)
            StartCoroutine(CRScaleAnim());
        yield return null;
    }
    #endregion



    #region 动画  控制翻转的适合进行放大和缩小 携程函数
    IEnumerator CRScaleAnim()
    {
        //缩小
        float time = 0.1f;
        float t = 0;
        while (t < time)
        {
            t += Time.deltaTime;
            float factor = t / time;
            transform.localScale = Vector3.Lerp(Vector3.one, new Vector3(0.5f, 0.5f, 0.5f), factor);
            yield return null;
        }

        //放大
        time = 0.1f;
        t = 0;
        while (t < time)
        {
            t += Time.deltaTime;
            float factor = t / time;
            transform.localScale = Vector3.Lerp(transform.localScale, Vector3.one, factor);
            yield return null;
        }
    }
    #endregion

}
